#include "GUI_TabBar.h"

#define GUI_TABBAR_BTN_POSI 55

static lv_obj_t * GUI_LeftTabBar_Btn(lv_obj_t * father,lv_img_dsc_t * img);
static void GUI_LeftTabBar_DeleteEvent_cb(lv_event_t * event);
static lv_style_t selectBtn_style;

lv_obj_t * GUI_MainTabBar_Create(lv_obj_t * father){
    char i;

    lv_obj_t * tabBar = lv_obj_create(father);
    lv_obj_add_event_cb(tabBar,GUI_LeftTabBar_DeleteEvent_cb,LV_EVENT_DELETE,NULL);
    GUI_Clear_Style(tabBar);
    lv_obj_set_size(tabBar,GUI_SCREEN_WIDTH,GUI_SCREEN_HEIGHT);
    lv_obj_set_style_bg_color(tabBar,GUI_THEME_BG_COLOR01,LV_STATE_DEFAULT);

    lv_obj_t ** card = malloc(sizeof(lv_obj_t *)*4);
    tabBar->user_data = card;
    for(i=0;i<4;i++){
        card[i] = lv_obj_create(tabBar);
        GUI_Clear_Style(card[i]);
        lv_obj_set_style_pad_all(card[i],10,LV_STATE_DEFAULT);
        lv_obj_set_align(card[i],LV_ALIGN_TOP_RIGHT);
        lv_obj_set_size(card[i],GUI_SCREEN_WIDTH-GUI_TABBAR_SIZE,GUI_SCREEN_HEIGHT);
        lv_obj_set_style_bg_color(card[i],GUI_THEME_BG_COLOR01,LV_STATE_DEFAULT);
    }
    lv_obj_move_foreground(card[0]);
    //lv_obj_set_style_bg_color(card[0],GUI_THEME_BG_COLOR01,LV_STATE_DEFAULT);

    lv_obj_t * bar = lv_obj_create(tabBar);
    GUI_Clear_Style(bar);
    lv_obj_set_size(bar,GUI_TABBAR_SIZE,GUI_SCREEN_HEIGHT);
    lv_obj_set_style_bg_color(bar,GUI_THEME_BG_COLOR02,LV_STATE_DEFAULT);

    lv_obj_t * btn[4];
    LV_IMG_DECLARE(temperature_32x32);
    btn[0] = GUI_LeftTabBar_Btn(bar,&temperature_32x32);
    LV_IMG_DECLARE(grid_32x32);
    btn[1] = GUI_LeftTabBar_Btn(bar,&grid_32x32);
    LV_IMG_DECLARE(statistics_32x32);
    btn[2]  = GUI_LeftTabBar_Btn(bar,&statistics_32x32);
    LV_IMG_DECLARE(setting_32x32);
    btn[3] = GUI_LeftTabBar_Btn(bar,&setting_32x32);
    for(i=0;i<4;i++){
        lv_obj_align(btn[i],LV_ALIGN_TOP_MID,0,20+GUI_TABBAR_BTN_POSI*i);
    }
    lv_obj_set_style_img_recolor(lv_obj_get_child(btn[0],0),GUI_THEME_ICON_COLOR02,LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(btn[0],GUI_THEME_BG_COLOR03,NULL);

    return tabBar;
}

lv_obj_t * GUI_MainTabBar_Get_Content(lv_obj_t * obj,char index){
    return ((lv_obj_t **)obj->user_data)[index];
}

static void GUI_LeftTabBar_DeleteEvent_cb(lv_event_t * event){
    free(event->target->user_data);
}

static void GUI_LeftTabBarBtn_ClickEvent_cb(lv_event_t * event){
    char i;
    lv_obj_t * btn = event->target;
    lv_obj_t * father = lv_obj_get_parent(btn);
    lv_obj_t * gradfather = lv_obj_get_parent(father);
    lv_obj_t ** card = (lv_obj_t *)gradfather->user_data;
    lv_obj_set_style_img_recolor(lv_obj_get_child(btn,0),GUI_THEME_ICON_COLOR02,LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(btn,lv_color_hex(0x222222),NULL);

    char count = lv_obj_get_child_cnt(father);
    for(i=0;i<count;i++){
        lv_obj_t * btnOther = lv_obj_get_child(father,i);
        if(btnOther == btn){
            printf("%d\n",i);
            //lv_obj_move_to_index(, lv_obj_get_child_cnt(gradfather) - 2);
            lv_obj_move_foreground(card[i]);
            continue;
        }
        lv_obj_set_style_img_recolor(lv_obj_get_child(btnOther,0),GUI_THEME_ICON_COLOR01,LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(btnOther,GUI_THEME_BG_COLOR02,NULL);
    }
}

static lv_obj_t * GUI_LeftTabBar_Btn(lv_obj_t * father,lv_img_dsc_t * img){
    lv_obj_t * btn = lv_obj_create(father);
    GUI_Clear_Style(btn);
    lv_obj_set_size(btn,40,40);
    lv_obj_set_style_radius(btn,5,LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(btn,GUI_THEME_BG_COLOR02,NULL);
    lv_obj_add_event_cb(btn,GUI_LeftTabBarBtn_ClickEvent_cb,LV_EVENT_CLICKED,NULL);

    lv_obj_t * Img_Btn = lv_img_create(btn);
    lv_img_set_src(Img_Btn,img);
    lv_obj_set_style_img_recolor(Img_Btn,GUI_THEME_ICON_COLOR01,LV_STATE_DEFAULT);
    lv_obj_set_style_img_recolor_opa(Img_Btn,150,LV_STATE_DEFAULT);
    lv_obj_center(Img_Btn);
    return btn;
}
